<template>
	<el-container>
		<el-header class="header-tabs">
			<el-tabs type="card" v-model="groupId" @tab-change="tabChange">
				<el-tab-pane label="采购单列表" name="1"></el-tab-pane>
				<el-tab-pane label="采购商品明细" name="2"></el-tab-pane>
			</el-tabs>
		</el-header>
		<el-header style="border-bottom: none;height: 50px;">
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'procure.add'" v-if="groupId == '1'"></el-button>
				<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del" v-auth="'procure.delete'" v-if="groupId == '1'"></el-button>
				<el-popconfirm title="确定审核吗？" @confirm="auditing" v-if="groupId == '1'">
					<template #reference>
						<!-- <el-button type="success" :disabled="selection.length == 0">批量审核</el-button> -->
						<el-button type="warning" plain v-auth="'procure.check'" :disabled="selection.length == 0">审核</el-button>
					</template>
				</el-popconfirm>
				<el-popconfirm title="确定反审核吗?" @confirm="batch_uncheck" v-if="groupId == '1'">
					<template #reference>
						<el-button type="info" plain v-auth="'procure.uncheck'" :disabled="selection.length == 0">反审核</el-button>
					</template>
				</el-popconfirm>
				<sc-export-button :auth="'procure.export'" :apiObj="apiObj" :search="search" v-if="groupId == 1"></sc-export-button>
				<sc-export-button :auth="'procure.detailsExport'" :apiObj="shopObj" :search="search" v-if="groupId == 2"></sc-export-button>
				<!-- 打印-->
				<sc-print-button :auth="'procure.print'" :selection="selection"></sc-print-button>
			</div>
		</el-header>
		<el-header style="height: 50px;">
			<div class="right-panel">
				<div class="right-panel-search">
					<el-date-picker v-model="search.create_time" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期"
									end-placeholder="结束日期"
									style="width: 230px"/>
					<el-input v-model="search.code" placeholder="采购单号" clearable @keyup.enter="upsearch"></el-input>
					<el-select v-model="search.apply_id" placeholder="请选择供应商" filterable clearable>
						<el-option v-for="item in applyList" :key="item.apply_id" :label="item.apply_name" :value="item.apply_id"></el-option>
					</el-select>
					<el-select v-model="search.purchase_staff" filterable clearable placeholder="请选择采购人">
						<el-option v-for="item in adminList" :key="item.id" :label="item.name" :value="item.id"/>
					</el-select>
					<el-select v-model="search.is_ticket" placeholder="是否开票" filterable v-if="groupId == 1" clearable>
						<el-option label="否" :value="1"></el-option>
						<el-option label="是" :value="2"></el-option>
					</el-select>
					<el-select v-model="search.is_tax" placeholder="是否含税" filterable v-if="groupId == 1" clearable>
						<el-option label="否" :value="0"></el-option>
						<el-option label="是" :value="1"></el-option>
					</el-select>
					<el-input v-model="search.goods_name" placeholder="请输入商品名称" clearable @keyup.enter="upsearch" v-if="groupId == 2"></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
					<el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding" v-if="groupId == 1">
			<scTable ref="table" :apiObj="apiObj" row-key="id" @selection-change="selectionChange" border>
				<el-table-column type="selection" width="50" align="center"></el-table-column>
				<el-table-column type="index" width="50" align="center"></el-table-column>
				<el-table-column label="采购单号" prop="code" align="center"></el-table-column>
				<el-table-column label="供应商" prop="apply.apply_name" align="center"></el-table-column>
				<el-table-column label="开单日期" prop="open_date" align="center"></el-table-column>
				<el-table-column label="数量" prop="number_all" align="center"></el-table-column>
				<el-table-column label="金额" prop="price_all" align="center"></el-table-column>
				<el-table-column label="入库进度" prop="" align="center"></el-table-column>
				<el-table-column label="付款进度" prop="" align="center"></el-table-column>
				<el-table-column label="是否开票" prop="is_ticket" align="center">
					<template #default="scope">
						<el-tag v-if="scope.row.is_ticket ==2" type="success">是</el-tag>
						<el-tag v-else type="danger">否</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="是否含税" prop="is_tax" align="center">
					<template #default="scope">
						<el-tag v-if="scope.row.is_tax == 1" type="success">是</el-tag>
						<el-tag v-else type="danger">否</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="采购人" prop="purchase.name" align="center"></el-table-column>
				<el-table-column label="创建人" prop="admin.name" align="center"></el-table-column>
				<el-table-column label="备注" prop="remark" align="center">
					<template #default="scope">
						<el-popover placement="top-start" :width="200" trigger="hover" :content="scope.row.remark">
							<template #reference>
								<el-button style="padding: 5px 0;font-size: 12px" text class="m-2">{{ scope.row.remark.substring(0, 10) }}</el-button>
							</template>
						</el-popover>
					</template>
				</el-table-column>
				<!-- <el-table-column label="销售编号" prop="in_number" align="center"></el-table-column> -->
				<el-table-column label="创建时间" prop="create_time" align="center"></el-table-column>
				<el-table-column label="审核状态" prop="status" align="center">
					<template #default="scope">
						<el-tag :type="scope.row.status == 0 ? 'warning' : scope.row.status == 1 ? 'success' : 'danger'">
							{{ scope.row.status == 0 ? '待审核' : scope.row.status == 1 ? '审核通过' : '审核驳回' }}
						</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" align="center" width="200">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)" v-auth="'procure.show'">查看</el-button>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'procure.edit'" v-if="scope.row.status == 0">编辑
							</el-button>
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="danger" size="small" v-auth="'procure.delete'">删除</el-button>
								</template>
							</el-popconfirm>
							<el-button text type="warning" size="small" @click="table_copy(scope.row, scope.$index)">复制订单</el-button>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
		<el-main class="nopadding" v-if="groupId == 2">
			<scTable ref="table" row-key="id" :apiObj="shopObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border>
				<el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
				<el-table-column type="index" label="序号" fixed align="center" width="60"></el-table-column>
				<el-table-column label="采购单号" prop="code" align="center"></el-table-column>
				<el-table-column label="供应商" prop="apply_name" align="center"></el-table-column>
                <el-table-column label="产品型号" prop="goods.goods_model" align="center"></el-table-column>
                <el-table-column label="颜色" prop="color_name" align="center"></el-table-column>
                <el-table-column label="方向" prop="direction_name" align="center"></el-table-column>
                <el-table-column label="速比" prop="goods.speed_ratio.name" align="center"></el-table-column>
                <el-table-column label="法兰" prop="goods.flange.name" align="center"></el-table-column>
                <el-table-column label="孔数" prop="goods.hole" align="center"></el-table-column>
                <el-table-column label="数量" prop="number" align="center"></el-table-column>
                <el-table-column label="备注" prop="remark" align="center"></el-table-column>
				<el-table-column label="采购人" prop="create_staff_name" align="center"></el-table-column>
				<el-table-column label="创建时间" prop="create_time" align="center"></el-table-column>
			</scTable>
		</el-main>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
	<check-status v-if="dialog.check" ref="checkDialog" @success="handleSuccess" @closed="dialog.check = false" draggable></check-status>
</template>

<script>
import CheckStatus from '@/views/ware/checkStatus.vue'
import saveDialog from './save'

export default {
	name: '采购计划管理',
	components: {
		saveDialog, CheckStatus,
	},
	data() {
		return {
			dialog: {
				save: false,
				check: false,
			},
			apiObj: this.$API.procure.order.list,
			shopObj: this.$API.procure.order.goodsLists,
			selection: [],
			search: {
				code: null,
				apply_id: null,
				purchase_staff: null,
				is_ticket: null,
				is_tax: null,
				goods_name:null,
				create_time:null
			},
			applyList: [],
			adminList: [],
			groupId: "1",
			form: {},
		}
	},
	mounted() {
		this.getApply();
		this.getAdmin();
	},
	methods: {
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		// 转采购单
		table_turn(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('turn').setData(row)
			})
		},
		//查看
		table_show(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row)
			})
		},
		//反审核
		async table_uncheck(row) {
			var reqData = {id: [row.id], status: 0}
			var res = await this.$API.contract.contract.check.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("操作成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//删除
		async table_del(row) {
			var reqData = {id: row.id}
			var res = await this.$API.procure.order.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async table_del_all(delete_ids) {
			var reqData = {id: delete_ids}
			var res = await this.$API.procure.order.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//批量删除
		async batch_del() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})

			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				this.table_del_all(delete_ids)
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			} else if (mode == 'copy') {
				this.$refs.table.refresh()
			} else if (mode == 'check') {
				this.check(data)
			}
		},
		async check(row) {
			var reqData = {id: row, status: 1}
			var res = await this.$API.procure.order.check.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("操作成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		refresh() {
			for (const key in this.search) {
				this.search[key] = null;
			}
			this.$refs.table.upData(this.search)
		},
		async auditing() {
			var ids = [];
			let go = true
			this.selection.forEach((item) => {
				if (item.status != 0) {
					go = false
				} else {
					ids.push(item.id)
				}
			})
			if (!go) {
				this.$message.error("请选择未审核的订单")
				return
			}
			var reqData = {
				id: ids,
				status: 1
			}
			var res = await this.$API.procure.order.check.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("审核成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async batch_uncheck() {
			var ids = [];
			let go = true
			this.selection.forEach((item) => {
				if (item.status != 1) {
					go = false
				} else {
					ids.push(item.id)
				}
			})
			if (!go) {
				this.$message.error("请选择已审核的订单")
				return
			}
			var reqData = {id: ids, status: 0}
			var res = await this.$API.procure.order.uncheck.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("操作成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		// 供应商
		async getApply() {
			var res = await this.$API.apply.apply.all.get();
			this.applyList = res.data;
		},
		// 获取采购人
		async getAdmin() {
			var res = await this.$API.system.admin.all.get();
			this.adminList = res.data;
		},
		// 复制新增
		async table_copy(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('copy').setCopy(row)
			})
		},
	}
}
</script>

<style>
</style>
